package cn.cvs.servlet;
import cn.cvs.dao.ResumeMapper;
import cn.cvs.pojo.Resume;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/ResumeAddServlet")
public class ResumeAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String userName = request.getParameter("userName");
        String phone = request.getParameter("phone");
        String gender = request.getParameter("gender");
        String birth= request.getParameter("birth");
        String address = request.getParameter("address");
        String school = request.getParameter("school");
        int status = Integer.parseInt(request.getParameter("status"));
        Resume resume = new Resume();
        resume.setUserName(userName);
        resume.setTelephone(phone);
        resume.setSex(gender);
        resume.setBirth(birth);
        resume.setAddress(address);
        resume.setSchool(school);
        resume.setStatus(status);
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        ResumeMapper mapper=sqlSession.getMapper(ResumeMapper.class);
        int iRet=mapper.insert(resume);
        if (iRet>0) {
            response.getWriter().print("  <script>" +
                    "    alert(\"添加成功！\");" +
                    "    location.href=\"ResumeListServlet\";" +
                    "  </script>");
        }else {
            response.getWriter().print("  <script>" +
                    "    alert(\"添加失败！\");" +
                    "    history.back();" +
                    "  </script>");
        }
        response.getWriter().flush();
        response.getWriter().close();
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
